package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.Utilities;

import dao.Teacher;
import dao.TeacherDAO;

public class TeacherManageServlet extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	this.doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	if (request.getQueryString() != null) {
	    String actionString = request.getParameter("action");
	    String teacherName = request.getParameter("teacherName").trim();
	    String sex = request.getParameter("sex").trim();
	    String birthDate = request.getParameter("birthDate").trim();
	    String grade = request.getParameter("grade").trim();
	    String courseName = request.getParameter("courseName").trim();
	    String rank = request.getParameter("rank").trim();
	    String description = request.getParameter("des").trim();

	    if ("add".equals(actionString)) {
		TeacherDAO teacherService = new TeacherDAO();
		Teacher teacher = new Teacher();
		DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
		    date = dFormat.parse(birthDate);
		    teacher.setBirthdate(date);
		} catch (ParseException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		teacher.setCourseid(grade + courseName);
		teacher.setDescription(description);
		teacher.setName(teacherName);
		teacher.setRank(rank);
		teacher.setSex(sex);
		String teaString = Utilities.getTeacherId(date);
		if (teacherService.findById(teaString) == null) {
		    teacher.setTeacherid(teaString);
		}
		if (teacherService.save(teacher)) {
		    request.setAttribute("success", "true");
		} else {
		    request.setAttribute("success", "false");
		}
		request.getRequestDispatcher("../admin/addTeacher.jsp")
			.forward(request, response);

	    } else if ("modify".equals(actionString)) {
		String teacherid = request.getParameter("teacherId").trim();
		TeacherDAO teacherService = new TeacherDAO();
		Teacher teacher = new Teacher();
		teacher.setCourseid(grade + courseName);
		teacher.setDescription(description);
		teacher.setName(teacherName);
		teacher.setRank(rank);
		teacher.setSex(sex);
		teacher.setTeacherid(teacherid);
		try {
		    DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
		    teacher.setBirthdate(dFormat.parse(birthDate));
		} catch (ParseException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		if (teacherService.update(teacher)) {
		    request.setAttribute("success", "true");
		} else {
		    request.setAttribute("success", "false");
		}
		request.getRequestDispatcher("../admin/modifyTeacher.jsp?teacherId="+teacher.getTeacherid())
			.forward(request, response);
	    }
	}
    }

}
